package com.ulic.customerManApplication.util.excel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.ulic.customerManApplication.commonmodule.service.OrganizationService;
import com.ulic.customerManApplication.memberenjoymodule.bo.MemberEnjoyActivity;
import com.ulic.customerManApplication.servlet.GetSpringBeanServlet;


public class ExpMemberEnjoys {
   
	public static void exp(String[] headers,List<Object> objects,String filePath){
		OrganizationService organizationService = (OrganizationService)GetSpringBeanServlet.getBean("organizationService");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
	    HSSFSheet sheet = workbook.createSheet();
	    workbook.setSheetName(0,"会员专享信息", (short)1);
	    sheet.setDefaultColumnWidth((short) 15);  
	    HSSFCellStyle style = workbook.createCellStyle();  
	      
	    style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
	    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
	    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
	    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
	    style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
	    style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  

	    HSSFFont font = workbook.createFont();  
	    font.setColor(HSSFColor.VIOLET.index);  
	    font.setFontHeightInPoints((short) 12);  
	    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
	        
	    style.setFont(font);  
	        
	    HSSFRow row = sheet.createRow(0);  
	    for (short i = 0; i < headers.length; i++)  //设置标题
	        {  
	          HSSFCell cell = row.createCell(i);  
	          cell.setCellStyle(style);  
	          HSSFRichTextString text = new HSSFRichTextString(headers[i]); 
	          cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          cell.setCellValue(text.toString());  
	         }  
	     int index = 1;   
	     MemberEnjoyActivity memberEnjoy = null;
	     for(Object object : objects){//填写数据
	    	  if(object instanceof MemberEnjoyActivity){
	    		  memberEnjoy = (MemberEnjoyActivity)object;
	    	  }
	          row = sheet.createRow(index++);
	          HSSFCell actDateCell = row.createCell((short)0);
	          actDateCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          actDateCell.setCellValue(memberEnjoy.getActivityDate());
	          
	          HSSFCell memActCell = row.createCell((short)1);
	          memActCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          memActCell.setCellValue(memberEnjoy.getEnjoyActivity());
	          
	          HSSFCell subCompanyCell = row.createCell((short)2); 
	          subCompanyCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          subCompanyCell.setCellValue(memberEnjoy.getSubComName());
	          
	          HSSFCell subCityCell = row.createCell((short)3); 
	          subCityCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          subCityCell.setCellValue(memberEnjoy.getSubCityName());
	          
	          HSSFCell orgFeeCardCell = row.createCell((short)4); 
	          orgFeeCardCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          orgFeeCardCell.setCellValue(memberEnjoy.getOrganizeFee());
	          
	          HSSFCell pNumCell = row.createCell((short)5); 
	          pNumCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          pNumCell.setCellValue(memberEnjoy.getParticipantsNum());
	          
	          HSSFCell actPlaceCell = row.createCell((short)6); 
	          actPlaceCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          actPlaceCell.setCellValue(memberEnjoy.getActivityPlace());
	          
	          HSSFCell oaCell = row.createCell((short)7); 
	          oaCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          oaCell.setCellValue(memberEnjoy.getOaSph());
	      }
	     OutputStream out  = null;    
		try {
			out= new FileOutputStream(filePath);//读出到服务器上的文件里
			workbook.write(out);
			out.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
